﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;

using TTBusiness.DB_Linq;
using TTBusiness;
namespace TTBusiness.ClassDB
{
    /// <summary>
    /// Create by ThiepND 23/3/2011
    /// </summary>
    public class clsBSCateFactory
    {
        TTMarketing_DBDataContext objDB = new TTMarketing_DBDataContext();
        public List<tblCate_Factory> getAll()
        {
            try
            {
                var list = from a in objDB.tblCate_Factories
                           select a;
                return list.ToList<tblCate_Factory>();
            }
            catch (Exception)
            {

                throw;
            }
        }
        public List<tblManuFactory> getFactoryByCate(int cateid)
        {
            try
            {
                var list = from a in objDB.tblCate_Factories
                           where a.CateID.Equals(cateid)
                           select a.tblManuFactory;
                return list.ToList<tblManuFactory>();
            }
            catch (Exception)
            {

                throw;
            }
        }

        public tblCate_Factory getById(int id)
        {
            try
            {
                var obj = objDB.tblCate_Factories.SingleOrDefault(a => a.ID == id);
                return obj;
                
            }
            catch (Exception)
            {

                throw;
            }
        }
        public tblCate_Factory getByFactoryAndCate(int factoryid,int cateid)
        {
            try
            {
                var obj = objDB.tblCate_Factories.SingleOrDefault(a => a.FactoryID==factoryid && a.CateID==cateid);
                return obj;

            }
            catch (Exception)
            {

                throw;
            }
        }

        public void insert(tblCate_Factory obj)
        {
            try
            {
                objDB.tblCate_Factories.InsertOnSubmit(obj);
                objDB.SubmitChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }

        public void update(tblCate_Factory obj)
        {
            try
            {
                var objj = objDB.tblCate_Factories.SingleOrDefault(a => a.ID == obj.ID);
                if (objj != null)
                {
                    objj.ID = obj.ID;
                    objj.CateID = obj.CateID;
                    objj.FactoryID = objj.FactoryID;
                    objDB.SubmitChanges();
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        
        public void delete(int id)
        {
            try
            {
                var obj = (from a in objDB.tblCate_Factories
                           where a.ID == id
                           select a).SingleOrDefault();
                objDB.tblCate_Factories.DeleteOnSubmit(obj);
                objDB.SubmitChanges();
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
